<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:variable name="smallCase" select="'abcdefghijklmnopqrstuvwxyz'"/>
  <xsl:variable name="upperCase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>

  <xsl:template match="/">
    <root>
      <NameMapping>
        <xsl:apply-templates select="/Nodes/Node"/>
      </NameMapping>
      <Aliases>
        <xsl:apply-templates select="//Node[@name='logical nodes']/Node" >
          <xsl:with-param name="isAlias" select="'A'"/>
        </xsl:apply-templates>
      </Aliases>
    </root>
  </xsl:template>

  <xsl:template match="Node">
    <xsl:param name="isAlias"/>

    
    
    <xsl:if test="not($isAlias='A')">
    
      <xsl:if test="starts-with(@name,'{')">
        <NM>
          <xsl:attribute name="name">
            <xsl:value-of select="Prp[@name='name']/@value"/>
          </xsl:attribute>
          <xsl:attribute name="guid">
            <xsl:value-of select="@name"/>
          </xsl:attribute>
          
          <xsl:apply-templates select="Node"/>
        </NM>
      </xsl:if>
      <xsl:if test="not(starts-with(@name,'{'))">
          <xsl:apply-templates select="Node"/>      
      </xsl:if>

    </xsl:if>




    <xsl:if test="$isAlias='A'">
      <A>
        <xsl:attribute name="name">
          <xsl:value-of select="Prp[@name='name']/@value"/>
        </xsl:attribute>
        <xsl:attribute name="guid">
          <xsl:value-of select="translate(Prp[@name='owner']/@value,$upperCase,$smallCase)"/>
        </xsl:attribute>

        <xsl:apply-templates select="Node" >
          <xsl:with-param name="isAlias" select="'A'"/>
        </xsl:apply-templates>

      </A>
    </xsl:if>
      
  </xsl:template>
  
</xsl:stylesheet>